package socket.tcp.demo2;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author 周宇
 * @create 2020-05-26 17:14
 */
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        //1.创建socket对象
        ServerSocket ss = new ServerSocket(8888);
        //2.等待客户端连接
        System.out.println("------------等待客户端连接----------------");
        Socket accept = ss.accept();
        System.out.println("------------客户端连接成功----------------");
        //3.获得输入流对象
        InputStream is = new DataInputStream(accept.getInputStream());
        StringBuilder sb = new StringBuilder();
        int b;
        while ((b = is.read()) != -1) {
            sb.append((char) b);
        }
        System.out.println(sb.toString());
        //4.释放资源
        is.close();
        ss.close();
    }
}
